﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
    public class Cus_BillController : Controller
    {

        private DataClassesVehicleDataContext dc = new DataClassesVehicleDataContext();
        //
        // GET: /Admin/Cus_Bill/

        public ActionResult Index()
        {
            List<CustomerBillingInformation> cbi = (from cb in dc.CustomerBillingInformations select cb).ToList();
            if(ModelState.IsValid)
            {
                return View(cbi);                
            }                        
            return View();
        }


        //
        // GET: /Admin/Cus_Bill/Details/5

        public ActionResult Details(int Bill)
        {
            CustomerBillingInformation cbd = (from cb in dc.CustomerBillingInformations.Where(cb => cb.billNo == Bill) select cb).First();
            if(ModelState.IsValid)
            {
                return View(cbd);
            }
            return View();
        }



        //
        // GET: /Admin/Cus_Bill/Create

        public ActionResult Create()
        {
            List<Account> listAccount = (from acc in dc.Accounts select acc).ToList();
            ViewData["listAccount"] = new SelectList(listAccount, "Acc_ID", "Acc_FullName");

            List<CustomerRecord> listCus = (from Cus in dc.CustomerRecords select Cus).ToList();
            ViewData["listCus"] = new SelectList(listCus, "customerID", "customerFullName");

            CustomerBillingInformation cbc = new CustomerBillingInformation();
            return View(cbc);
        } 

        //
        // POST: /Admin/Cus_Bill/Create

        [HttpPost]
        public ActionResult Create(CustomerBillingInformation cb)
        {
            List<Account> listAccount = (from acc in dc.Accounts select acc).ToList();
            ViewData["listAccount"] = new SelectList(listAccount, "Acc_ID", "Acc_FullName");

            List<CustomerRecord> listCus = (from Cus in dc.CustomerRecords select Cus).ToList();
            ViewData["listCus"] = new SelectList(listCus, "customerID", "customerFullName");

           if(ModelState.IsValid)
           {
               dc.CustomerBillingInformations.InsertOnSubmit(cb);
               dc.SubmitChanges();
               RedirectToAction("Index");
           }
           return View();
        }
        
        //
        // GET: /Admin/Cus_Bill/Edit/5

        public ActionResult Edit()
        {
            List<Account> listAccount = (from acc in dc.Accounts select acc).ToList();
            ViewData["listAccount"] = new SelectList(listAccount, "Acc_ID", "Acc_FullName");

            List<CustomerRecord> listCus = (from Cus in dc.CustomerRecords select Cus).ToList();
            ViewData["listCus"] = new SelectList(listCus, "customerID", "customerFullName");

            CustomerBillingInformation cb = new CustomerBillingInformation();
            return View(cb);
        }

        //
        // POST: /Admin/Cus_Bill/Edit/5

        [HttpPost]
        public ActionResult Edit(int bill, CustomerBillingInformation cbe)
        {
            try
            {
                CustomerBillingInformation edit = dc.CustomerBillingInformations.Where(cbb => cbb.billNo == bill).First();
                List<Account> listAccount = (from acc in dc.Accounts select acc).ToList();
                ViewData["listAccount"] = new SelectList(listAccount, "Acc_ID", "Acc_FullName");

                List<CustomerRecord> listCus = (from Cus in dc.CustomerRecords select Cus).ToList();
                ViewData["listCus"] = new SelectList(listCus, "customerID", "customerFullName");

                edit.date = cbe.date;
                edit.amount = cbe.amount;
                edit.Acc_ID = cbe.Acc_ID;
                edit.customerID = cbe.customerID;
                edit.TypePayment = cbe.TypePayment;
                edit.Bank = cbe.Bank;
                edit.BankAccount = cbe.BankAccount;

                dc.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        //
        // GET: /Admin/Cus_Bill/Search/5

        public ActionResult Search()
        {
            List<CustomerBillingInformation> cbs = new List<CustomerBillingInformation>();
            return View();
        }

        [HttpPost] 
        public ActionResult Search(string bill)
        {
            List<CustomerBillingInformation> cbs = (from ac in dc.CustomerBillingInformations.Where(cbb => cbb.billNo.ToString().Contains(bill)) select ac).ToList();
            return View(cbs);
        }
                                                                    
    }
}
